﻿using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using AMS.DeDuplicator.CommonLayer;
using AMS.DeDuplicator.CommonLayer.Adapter;
using AMS.DeDuplicator.CommonLayer.Iterator;

namespace AMS.DeDuplicator.BusinessLayer.Command
{
    public class DeleteReplaceFacade : Command
    {
        IIterator<string> _selectedTargets;

        List<MoveToTempCommand> _moves;
        List<CreateShortcutCommand> _shortcuts;

        #region Constructors
        public DeleteReplaceFacade(IIterator<ISelectableText> allTargets)
            : base()
        {
            InputValidator.ValidateNotNull(allTargets);
            PopulateTargets(allTargets);

        }

        public DeleteReplaceFacade(IIterator<string> selectedTargets)
            : base()
        {
            InputValidator.ValidateNotNull(selectedTargets);
            _selectedTargets = selectedTargets;
        }

        private void PopulateTargets(IIterator<ISelectableText> targets)
        {
            List<string> holder = new List<string>();
            while (targets.HasNext())
            {
                ISelectableText next = targets.GetNext();
                if (next.Selected)
                {
                    holder.Add(next.Text);
                }
            }
            _selectedTargets = new EnumeratorAdapter<string>(holder.GetEnumerator());
        } 
        #endregion

        public override bool Execute()
        {
            if (this.Executed)
                ExceptionHandler.AlreadyExecutedException();

            if (_moves == null)
            {
                return FirstExecute();
            }
            else // redo
            {
                foreach (MoveToTempCommand move in _moves)
                {
                    move.Execute();
                }

                foreach (CreateShortcutCommand shortcut in _shortcuts)
                {
                    shortcut.Execute();
                }
            }

            this.Executed = true;
            return true;
        }

        private bool FirstExecute()
        {
            _moves = new List<MoveToTempCommand>();
            _shortcuts = new List<CreateShortcutCommand>();

            while (_selectedTargets.HasNext())
            {
                string next = _selectedTargets.GetNext();

                // move to Temp
                MoveToTempCommand move = new MoveToTempCommand(next);
                if (move.Execute()) // only add successful moves to undo/redo pool
                    _moves.Add(move);
                else
                    continue;

                // create shortcut (needs work)
                //CreateShortcutCommand shortcut = new CreateShortcutCommand(primeFile, dir, newName);
                //shortcut.Execute();
                //_shortcuts.Add(shortcut);
            }

            this.Executed = true;
            return true;
        }

        //public override bool Execute(object parameter)
        //{
        //    return Execute(); // ignore parameter
        //}

        public override bool Undo()
        {
            if (!this.Executed)
                ExceptionHandler.NotExecutedException();

            // undo all moves and shortcuts in inverse order
            for (int i = _moves.Count - 1; i >= 0; i--)
            {
                _moves[i].Undo();
            }

            for (int i = _shortcuts.Count - 1; i >= 0; i--)
            {
                _shortcuts[i].Undo();
            }

            return true;
        }
    }
}
